package com.wcsy.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcsy.myblog.dao.UserMapper;
import com.wcsy.myblog.entity.JsonResult;
import com.wcsy.myblog.entity.Label;
import com.wcsy.myblog.entity.User;
import com.wcsy.myblog.service.ILabelService;
import com.wcsy.myblog.service.serviceImpl.NumServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 标签信息
 */
@RestController
@RequestMapping("/label")
@CrossOrigin
@Api(description = "标签信息")
@ApiModel
public class LabelController {
    @Autowired
    private ILabelService iLabelService;
    @Autowired
    HttpSession session;
    @Autowired
    NumServiceImpl numService;
    @Autowired
    UserMapper userMapper;
    /**
     * @apiNote 查询用户的总标签
     * */
    @ApiOperation("查询用户的总标签")
    @GetMapping("/list")
    public JsonResult list(){
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", 1));
//        User user = (User) session.getAttribute("user");
        try {
            List<Label> labels = iLabelService.listLabel((int) user.getUserId());
            return new JsonResult(200,"查询成功",labels);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败",null);
        }
    }

    /**
     * @apiNote 添加标签
     * @apiNote label
     * @return
     */
    @ApiOperation("添加标签")
    @GetMapping("insert")
    public JsonResult insert(Label label){
        try {
             iLabelService.insertLable(label);
            return new JsonResult(200,"添加成功","");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"添加失败!请勿输入敏感词汇","");
        }
    }

    /**
     * @apiNote 修改标签
     * @param labelName 新标签名字
     * @param labelId 标签id
     * @return
     */
    @ApiOperation("修改标签")
    @GetMapping("update")
    public JsonResult update(String labelName,int labelId){
        String getfilter = numService.getfilter(labelName);
        if (!getfilter.equals(labelName)){
            return new JsonResult(500,"修改失败!请勿输入敏感词汇","");
        }
        boolean b = iLabelService.updateLabel(labelName, labelId);
        if (b){
            return new JsonResult(200,"修改成功",labelName);
        }
        return new JsonResult(500,"修改失败!","");
    }
}
